/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.CategoryDao;
import gemini.basic.dao.GmnDao;
import gemini.basic.model.ProductType;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class CategoryDaoImpl extends HibernateDaoSupport implements CategoryDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public List<ProductType> getAll() {

		DetachedCriteria dtCri = DetachedCriteria.forClass(ProductType.class);

		List<Object> categoryList = gmnDao.findByCriteria(dtCri);
		
		List<ProductType> result = null;

		if (categoryList != null && !categoryList.isEmpty()) {
			result = new ArrayList<ProductType>(categoryList.size());
			ProductType cat = null;
			for (Object ob : categoryList) {
				cat = (ProductType)ob;
				result.add(cat);
			}
		}
		
		return result;
	}

	@Override
	public ProductType getByCode(String code) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(ProductType.class);

		if (code != null && !("".equals(code))) {
			dtCri.add(Restrictions.eq("code", code));
		}
		List<Object> categoryList = gmnDao.findByCriteria(dtCri);
		
		ProductType result = null;

		if (categoryList != null) {
			result = new ProductType();
			result = (ProductType)categoryList.get(0);
		}
		
		return result;
	}

	@Override
	public ProductType getById(Integer id) {
		ProductType result = getHibernateTemplate().get(ProductType.class, id);
		return result;
	}

	@Override
	public void delete(ProductType productType) {
		getHibernateTemplate().delete(productType);
	}

	@Override
	public ProductType saveOrUpdate(ProductType productType, boolean flush) {
		ProductType result = null;
		if (flush) {
			result = (ProductType) gmnDao.saveOrUpdateAndFlush(productType);
		} else {
			result = (ProductType) gmnDao.saveOrUpdate(productType);
		}
		return result;
	}
	
}
